package com.parking.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.parking.platform.dto.InterfaceDto;
import com.parking.platform.dto.InterfaceDtos;
import com.parking.platform.entity.PlatformInterface;
import com.parking.platform.dao.PlatformInterfaceMapper;
import com.parking.platform.entity.PlatformRoleResource;
import com.parking.platform.form.UpInterfaceForm;
import com.parking.platform.form.UpRoleResourceForm;
import com.parking.platform.service.PlatformInterfaceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 香菜狗带
 * @since 2023年12月06日
 */
@Service
public class PlatformInterfaceServiceImpl extends ServiceImpl<PlatformInterfaceMapper, PlatformInterface> implements PlatformInterfaceService {
    @Resource
    private PlatformInterfaceMapper platformInterfaceMapper;
    /***
     * 修改动态接口资源
     * 参数：
     * platformRoleId：角色Id
     * platformInterfaceId：接口Id
     */
    @Override
    @Transactional
    public void upPlatformInterface(List<UpInterfaceForm> form) {
        List<Integer> interId = form.stream().map(UpInterfaceForm::getPlatformInterfaceId).collect(Collectors.toList());
        if (ObjectUtil.isNotEmpty(interId)) {
            Integer roleId = form.get(0).getPlatformRoleId();
            QueryWrapper<PlatformInterface> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("platform_role_id",roleId);
            List<PlatformInterface> list = platformInterfaceMapper.selectList(queryWrapper);
            List<Integer> oldInter = list.stream().map(x -> x.getPlatformInterfaceId()).collect(Collectors.toList());
            List<Integer> differentElements = Stream.concat(
                    interId.stream().filter(e -> !oldInter.contains(e)),
                    oldInter.stream().filter(e -> !interId.contains(e))
            ).collect(Collectors.toList());
            for (Integer different : differentElements) {
                if (interId.contains(different)) {
                    PlatformInterface platformInterface = new PlatformInterface();
                    platformInterface.setPlatformInterfaceId(different);
                    platformInterface.setPlatformRoleId(roleId);
                }else {
                    QueryWrapper<PlatformInterface> wrapper = new QueryWrapper<>();
                    wrapper.eq("platform_role_id",roleId);
                    wrapper.eq("platform_interface_id",different);
                    platformInterfaceMapper.delete(wrapper);
                }
            }

        }


    }
    /***
     * 查询全部接口资源
     * 参数：
     * platformRoleId：角色Id
     *
     */
    @Override
    public InterfaceDtos findInterfaceAll(Integer platformRoleId) {
        InterfaceDtos dtos = new InterfaceDtos();
        QueryWrapper<PlatformInterface> wrapper = new QueryWrapper<>();
        List<PlatformInterface> list = platformInterfaceMapper.selectList(wrapper);
        List<InterfaceDto> dto = BeanUtil.copyToList(list, InterfaceDto.class);
        QueryWrapper<PlatformInterface> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("platform_role_id",platformRoleId);
        List<PlatformInterface> list2 = platformInterfaceMapper.selectList(wrapper2);
        List<Integer> collect = list2.stream().map(o -> o.getPlatformInterfaceId()).collect(Collectors.toList());
        // 创建 InterfaceDtos 对象并设置相应属性
        InterfaceDtos interfaceDtos = new InterfaceDtos();
        interfaceDtos.setInterfaceId(collect);
        interfaceDtos.setList(dto);
        return interfaceDtos;
    }
}
